import { _decorator, Button, Component, Node, Sprite, SpriteFrame } from 'cc';
import LoaderManager from 'db://app/manager/loader/LoaderManager';
import { BodyPartType } from './MainPageTabBtn';
import { app } from 'db://assets/app/app';
const { ccclass, property } = _decorator;

/**
 * 主界面item数据
 */
export interface MainPageViewItemData {
    part_type: BodyPartType;

    icon_id: number;

    select_status: boolean

    video_status: boolean;

    new_status: boolean;
}

@ccclass('MainPageViewItem')
export class MainPageViewItem extends Component {

    //#region 配置

    /**
     * 按钮组件
     */
    @property(Button)
    private btn_comp:Button;

    /**
     * 图标
     */
    @property(Sprite)
    private img_icon: Sprite;

    /**
     * 选中节点
     */
    @property(Node)
    private go_select: Node;

    /**
     * 看视频节点
     */
    @property(Node)
    private go_video: Node;

    /**
     * 新的物品图片节点
     */
    @property(Node)
    private go_new: Node;

    /**
     * 数据
     */
    private data: MainPageViewItemData;

    /**
     * 点击回调
     */
    private call_back:(icon_id: number, part_type: BodyPartType) => void;
    //#endregion


    start(): void {
        this.setSelect(false);
        this.setNew(false);
        this.setVideo(false);

        this.btn_comp.node.off('click');
        this.btn_comp.node.on('click', this.onClickFunc, this);
    }

    update(deltaTime: number): void {

    }

    private onClickFunc():void{
        this.call_back?.(this.data.icon_id,this.data.part_type)
    }

    /**
     * 设置数据
     * @param data 数据
     */
    public setData(data: MainPageViewItemData): void {
        this.data = data;
        console.log("【MainPageViewItem】输出 ===>>> setData:", data)

        this.setIcon(this.data.icon_id, this.data.part_type);
        this.setSelect(this.data.select_status);
        this.setVideo(this.data.video_status);
        this.setNew(this.data.new_status)
    }

    /**
     * 设置图标
     * @param id 
     */
    private setIcon(id: number, part: BodyPartType): void {
        let res_path = `sprites/part_icon/clothes/clothes_${id}/spriteFrame`
        switch (part) {
            case BodyPartType.clothes:
                res_path = `sprites/part_icon/clothes/clothes_${id}/spriteFrame`
                break;
            case BodyPartType.eyes:
                res_path = `sprites/part_icon/eyes/eyes_${id}/spriteFrame`
                break;
            case BodyPartType.hair:
                res_path = `sprites/part_icon/hair/hair_${id}/spriteFrame`
                break;
            case BodyPartType.shoes:
                res_path = `sprites/part_icon/shoes/shoes_${id}/spriteFrame`
                break;
            case BodyPartType.stockings:
                res_path = `sprites/part_icon/stockings/stockings_${id}/spriteFrame`
                break;
            default:
                break;
        }
        app.manager.loader.load({
            path: res_path, bundle: 'res-native', type: SpriteFrame, onComplete: (spriteFrame) => {
                this.img_icon.spriteFrame = spriteFrame
            }
        })
    }

    /**
     * 设置是否显示
     * @param statu 
     */
    private setSelect(statu: boolean): void {
        this.go_select.active = statu;
    }

    /**
     * 添加函数
     * @param callfunc 
     */
    public addCallBack(callfunc:(icon_id: number, part_type: BodyPartType) => void):void{
        this.call_back=callfunc;
    }

    /**
     * 设置是否显示
     * @param statu 
     */
    private setVideo(statu: boolean): void {
        this.go_video.active = statu;
    }

    /**
     * 设置是否显示
     * @param statu 
     */
    private setNew(statu: boolean): void {
        this.go_new.active = statu;
    }
}


